import { Component, OnInit, ElementRef, Input } from '@angular/core';
import { notifyAnimation } from '../shared/animation/index';

export var typeMap:any = {
  success: 'success',
  info: 'info',
  warning: 'warning',
  error: 'error',
};

@Component({
  selector: 'el-notification-container',
  animations: [notifyAnimation],
  templateUrl: './notification.component.html',
})
export class NotificationComponent implements OnInit {
  private el: any;
  @Input() id: string = '';
  @Input() height: number = 0;
  @Input() offset: number = 15;
  @Input() type: string = '';
  @Input() duration: number = 3000;
  @Input() iconClass: string = '';
  @Input() customClass: string = '';
  @Input() zIndex: number = 1000;
  @Input() position: string = 'top-right';
  @Input() title: string = '';
  @Input() message: string = '';
  @Input() showBox: boolean = false;
  @Input() timer: any;
  @Input() horizontalDirection: string = 'right';
  @Input() onClose: any = function () { };
  @Input() onDestroy: any = function () { };
  constructor(el: ElementRef) {
    this.el = el;
  };

  ngOnInit(): void {
    this.horizontalDirection = this.position.includes('right') ? 'right' : 'left';
  }


  makeClass() {
    return typeMap[this.type] ? "el-icon-" + typeMap[this.type] : '';
  }

  setContent(message:string, title:string){
    var _this = this;
    if (title === void 0) { title = ''; }
    this.message = message;
    this.title = title;
    setTimeout(function () {
        _this.height = _this.el.nativeElement.children[0].offsetHeight;
    }, 0);
  }

  show(){
    var _this = this;
        this.showBox = true;
        this.timer = setTimeout(function () {
            _this.close();
        }, this.duration);
  }

  close(){
    this.timer && clearTimeout(this.timer);
        this.showBox = false;
        this.onClose();
        this.onDestroy();
  }

  startTimer(){
    var _this = this;
        if (!this.showBox)
            return;
        this.timer = setTimeout(function () {
            _this.close();
        }, this.duration);
  }

  clearTimer(){
    this.timer && clearTimeout(this.timer);
  }
}
